/**
 * 
 */
package com.zxc.alipay;

import java.util.Random;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundAuthOrderVoucherCreateModel;
import com.alipay.api.request.AlipayFundAuthOrderVoucherCreateRequest;
import com.alipay.api.response.AlipayFundAuthOrderVoucherCreateResponse;

/**
 * @author admin
 *
 */
public class FundAuthOrderFreezeSandBoxDemo {

	static String url = "https://openapi.alipaydev.com/gateway.do";
	
	static String appid = "2016091600525593";
	
	static String merchantid = "2088102175906654";
	
	static String alipayLogonId = "iqbtec2643@sandbox.com";
	
	static String merchantPrivate="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCQX2PD38JHSUkRdIrePFFrhuGx5G+47IN70LBrTZUqeH9VqCwqRbH19HynxeyhaxSaAPzIDFMaUUe9xqTYd2UoxfGQygZnM3sgjyCoWBPYvEBb9QN+7TQk1V3eqJGTbN+ZYTN7h2Gl+8oy4N/aRqOVxL+0re6F41rwSpWsuFAYHnEi7+rhjYKdWRZIz+U+O142zDjzVnaXqHn7g73IYZC8rPFxHpD8KvjBE1X01ejPunDsXDG6f1GtqHge19w42yrhh3Jlhjv7XNdc34hmpy0LtAFbeS5HRi/HF3uEBLlHZH1sQeaXmdiwh6gQP3Ab5jVdggqluFKjWwmovxfCEt6BAgMBAAECggEANBsJYbdPYfUUNwWN3Rl99UXwDQk6PU63QaeXFJMio/prUpnonrUavn8nwlGPzs5b2B1FHlo8YAI310lrU2j8DJQsVCpl1zdgV+bwTXHEb/O5crbiNRwdNrH/OA40xDWGGdBYCVxaJNLULjq08A2QmswwuGAR9yaQOqtgi77VwW4oEhOuSiD9ioDh785jCNhDno8MLsHPYrIVKNZZzlZD8nj248UlIyoLSKViIC4ddv/yk22cz5NiYC6pcR5H7CI8ixe9cfwOQsnlI9wp8dMJkjS+MSWB8OMYtXxh7jP2dbTv2REen4wwSGHoTZdt+qLaIWxcQqte0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1Zeb5F2xC4TABqb2M8vgk71bZQPeKbgTQVvL8tJA5Qe9VyJfjUzA0nlKWPPLkjjrFcbU1F0W33MKqevlWWcDGlQB/R9dZ3Vp2slDzHNJ/LFtj5IBoqRGVrT3t94EE9nTx5H5I6esNpHJe0s6Ts5xSpFTQ04b7RW0lrte5GjA3sZZjE1WpDz7WnZenh+AYOuXL2DSlwIDAQAB";
	
	
	
	public static void main(String[] args) throws AlipayApiException {
		fundAuthOrderVoucherCreate();
	}
	
	public static void fundAuthOrderVoucherCreate() throws AlipayApiException{
		AlipayClient alipayClient = new DefaultAlipayClient(url,appid,merchantPrivate,"json","utf-8",alipayPublic,"RSA2");
		
		AlipayFundAuthOrderVoucherCreateRequest  request  =  new  AlipayFundAuthOrderVoucherCreateRequest();
		AlipayFundAuthOrderVoucherCreateModel  model  =  new  AlipayFundAuthOrderVoucherCreateModel();
		model.setOutOrderNo(getOutOrderNo());  //  预授权冻结交易商户订单号，商户系统唯一标识一笔预授权
		model.setOutRequestNo(getOutRequestNo());  //  商户授权资金操作流水号，不能重复
		model.setOrderTitle("酒店预授权冻结测试");  //  预授权标题,会在支付宝账单中显示
		model.setAmount("10");  //  预授权金额，注意需要大于等于结算支付金额
		model.setPayeeLogonId(alipayLogonId);
		model.setPayeeUserId(merchantid);  //  收款方的支付宝唯一用户号,以2088开头的16位纯数字组成，收款方支付宝登录号(payee_logon_id)和用户号(payee_user_id)不能同时为空。
		model.setPayTimeout("5m");  //  预授权冻结超时时间，超时则自动关闭冻结交易
		model.setProductCode("PRE_AUTH");  //  填写固定值PRE_AUTH	
		request.setBizModel(model);
		AlipayFundAuthOrderVoucherCreateResponse  response  =  alipayClient.execute(request);
		
		System.out.println("response:  {}"+response.getBody());
	}
	
	public static String getOutOrderNo(){
		return "jp"+getGuid();
	}
	
	public static String getOutRequestNo(){
		return "qp"+getGuid();
	}
	
	private static final Random RANDOM = new Random();// 用于GUID生成
	public static String getGuid() {
		StringBuffer buf = new StringBuffer();
		long mills = System.currentTimeMillis() % 100000000000l;
		long seed = Math.abs(RANDOM.nextInt()) % 10000;
		long seed2 = Math.abs(RANDOM.nextInt()) % 10000;
		while (seed2 == seed) {
			seed2 = Math.abs(RANDOM.nextInt()) % 10000;
		}
		seed = seed > 9 ? seed > 99 ? (seed > 999 ? seed : seed * 10)
				: seed * 100 : seed > 0 ? seed * 10 : 9999;
		seed2 = seed2 % 100;
		seed2 = seed2 > 9 ? seed2 : seed2 > 0 ? seed2 * 10 : 99;
		buf.append(mills).append(seed).append(seed2);
		return buf.toString();
	}
}
